﻿using Autofac;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using WebTest.Core.Entities;
using WebTest.Core.Interfaces;
using WebTest.Infrastructure.Data;

namespace WebTest.Core.Services
{
    public class MemberSignService : IMemberSignService
    {
        private readonly IServiceProvider service;
        //private readonly ILogger<MemberSignService> logger;
        
        public MemberSignService(IServiceProvider service/*, Logger<MemberSignService> logger*/)
        {
            this.service = service ?? throw new ArgumentNullException(nameof(service));
            //this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        public async Task<Member> SignIn(string username, string password)
        {
            using (var context = service.GetRequiredService<OrderContext>())
            {
                return await context.Set<Member>()
                    .SingleOrDefaultAsync(x => x.Account == username && x.Password == Member.CreateHashPassword(password));
            }
        }
    }
}
